<%@page language="java" session="true" pageEncoding="UTF-8"%>
<%
wt.httpgw.URLFactory urlFactory = new wt.httpgw.URLFactory();
String url = urlFactory.getBaseHREF();
String numberId = request.getParameter("numberId");
out.println("<input type='hidden' id='numberId' name='numberId' value='" + numberId + "'/>");
%>
<script type='text/javascript' src="<%=url%>netmarkets/javascript/ext421/ext-all.js" type="text/javascript"></script>
<script type='text/javascript' src="<%=url%>netmarkets/javascript/ext421/locale/ext-lang-zh_CN.js" type="text/javascript"></script>
<link href="<%=url%>netmarkets/javascript/ext421/resources/css/ext-all.css" type="text/css" rel="stylesheet">
<script type="text/javascript">
	Ext.onReady(function(){
		var states = Ext.create('Ext.data.Store', {
		    fields: ['innerName', 'displayName'],
		    autoLoad: true,
		    proxy: {
		        type: "ajax",
		        actionMethods: { read: "POST" },
		        url: "<%=url%>app/engineConfigDocController/queryProductSerial",
		        reader: {
		            type: "json"
		        }
		    }
		});
		
		var win = Ext.create('Ext.window.Window', {
		    title: '选择发动机配置号',
		    height: 400,
		    width: 900,
		    closable: false,
		    resizable:false,
		    padding:'15 5 3 10',
		    renderTo: Ext.getBody(),
		    layout: {
		        type: 'table',
		        columns: 6// The total column count must be specified here
		    },
		    items: [{
		    	xtype: 'tbtext', text: '&nbsp;&nbsp;&nbsp;'
		    },{
		    	id:'n_1',
		    	xtype: 'combo',
				fieldLabel : '选择产品系列',
				store : states,
				queryMode : 'remote',
				displayField : 'displayName',
				valueField : 'innerName'
			},{
		    	xtype: 'tbtext', text: '&nbsp;&nbsp;'
		    },{
				id:'n_2',
		        xtype: 'textfield',
		        name: 'pz',
		        fieldLabel: '固定值',
		        value:'PZ',
		        readOnly:'true'
		    },{
		    	xtype: 'tbtext', text: '&nbsp;&nbsp;'
		    },{
		    	id:'n_3',
		        xtype: 'numberfield',
		        name: 'pz',
		        fieldLabel: '流水码',
		        value:'',
		        maxValue: 9999,
		        minValue: 1
		    }],
		    bbar: [
		           {xtype:'tbfill'},
		           {xtype: 'button', text: '确认',handler:function(){
		        	   var n1 = Ext.getCmp("n_1").value;
		        	   var n2 = Ext.getCmp("n_2").value;
		        	   var n3 = Ext.getCmp("n_3").value;
		        	   if(n1 == null || n1 == "" || n2 == null || n2 == "" || n3 == null || n3 == ""){// 
		        		   Ext.Msg.alert("提示","显示框的值都需要填写！");
		        		   return;
		        	   }
		        	   if(n3 > 9999){
		        		   Ext.Msg.alert("提示","流水码的最大值为9999，请重新输入！");
		        		   return;
		        	   }
		        	   var numberId = document.getElementById("numberId").value;
		        	   var name = opener.document.getElementById(numberId);
		        	   n3 = Ext.String.leftPad(n3, 4, '0');
		        	   name.value = n1 + "-" + n2 + "-" + n3;
		        	   //更新数据库信息，将最大值更新
		        	   Ext.Ajax.request({
					   		url: '<%=url%>netmarkets/jsp/ext/weichai/doc/pname/updateMaxNumber.jsp',
						    params: {
						        maxNumber : n3,
						        rule : Ext.getCmp("n_1").value
						    },
						    success: function(response){
						        var responseText = response.responseText;
						        if(responseText != null && responseText != ""){
						        	var res = Ext.JSON.decode(responseText);
						        	if(res.result == "failure"){
						        		name.value = "";
						        		Ext.Msg.alert("提示",res.msg);
						        		return;
						        	}else if(res.result == "success"){
						        		//alert("更新产品系列" + Ext.getCmp("n_1").value + "的最大值成功！");
						        		window.close();
						        	}
						        }else{
						        	Ext.Msg.alert("提示","更新产品系列" + Ext.getCmp("n_1").value + "的最大值失败，请联系系统管理员！");
						        	//window.close();
						        }
						    },
						    failure : function() {
						    	Ext.Msg.alert("警告","更新最大值失败，请联系管理员！");
								window.close();
							}
						});
		           }},
		           {xtype:'tbfill'}
		    ]
		}).show();
		
		win.setPosition(300, 500);   
		win.fitContainer(); // 填充满浏览器   
		
		Ext.getCmp("n_1").on("change",function(){
			var productSerial = Ext.getCmp("n_1").value;
			Ext.Ajax.request({
				params : {
					productSerial : productSerial
				},
				url : "<%=url%>app/engineConfigDocController/productSerialSequence",
				method : "post",
				success : function(response, options) {
					var maxNumber = response.responseText;
					Ext.getCmp("n_3").setValue(maxNumber);
				},
				failure : function() {
					window.close();
				}
			});
		});
	});
</script>